'''
Created on Oct 11, 2012

@author: daeheehan
'''
from pprint import pprint
from github3 import login, GitHub
from getpass import getpass, getuser

import datetime
import dateutil.parser
import time
from pprint import pprint
import sys

# Github User information
GITHUB_USER_ID = "booknara"
GITHUB_USER_ID_1 = "booknaratest"
GITHUB_USER_ID_2 = "jangho87"

GITHUB_USER_PW = "qwer1234"

if __name__ == '__main__':
    pass

gh = login(GITHUB_USER_ID, password=GITHUB_USER_PW)
print gh.ratelimit_remaining, 'request remaining'

# Github html5 boilerplate Repository information
GITHUB_USER_NAME = 'h5bp'
GITHUB_REPOSITORY_NAME = 'html5-boilerplate'

# html5 boilerplate collaborators information file
CONTRIBUTORS_FILE = 'html5_boilerplate_contributors.txt'

# html5 boilerplate migrants_debutants information file
INFLOW_CONTRIBUTORS_FILE = 'html5_boilerplate_inflow_contributors.txt'

project_contributors = []
inflow_contributors_dict = {}

try:
    # Github html5 boilerplate project collaborators list from files
    f_collaborators = open(CONTRIBUTORS_FILE, 'r')
    
    for line in f_collaborators:
        if not line.strip():
            continue
        # We got something, save it
        else:
            tuple_list = line.split(',')
            
            login = tuple_list[1].strip()
            start_offset = login.find(':')
            user_login = login[start_offset+1:].strip()
#            print user_login
            project_contributors.append(user_login)

    f_collaborators.close()
    
    print 'loading contributors file is finished'
    
    repos = gh.repository(GITHUB_USER_NAME, GITHUB_REPOSITORY_NAME)
    
    for item in project_contributors:
        print 'login id: ' +  item

        try:
            repo_commit_list = list(repos.iter_commits(author=item, number = -1))
            if not repo_commit_list:
                print 'no commit information'
                continue

            repo_firstcommit = repo_commit_list[0]             
            # Checking what if there is no commit history
            repo_first_commit_date = repo_firstcommit.commit.author['date']
            first_date = dateutil.parser.parse(repo_first_commit_date).replace(tzinfo=None)
            time = str(first_date.year) + "/" + str(first_date.month)

            print 'First commit date: ', time
            
        except Exception as e: 
            print 'The 1st Exception' + str(e.args[0])
        
        if time not in inflow_contributors_dict.keys():
            inflow_contributors_dict[time] = 1
        else:
            inflow_contributors_dict[time] = inflow_contributors_dict.get(time,1) + 1

        if(gh.ratelimit_remaining < 100):
            time.sleep(60 * 60)
#        break

    f_inflow_contributors = open(INFLOW_CONTRIBUTORS_FILE, 'w')
    print 'Printing dictionary'
    for key, value in inflow_contributors_dict.items():
        sys.stdout.write(key + ", " + str(value) + '\n')
        f_inflow_contributors.write(key + ", " + str(value) + '\n')

    f_inflow_contributors.close()

except Exception as e: 
    print 'The final Exception' + str(e.args[0])

print str(gh.ratelimit_remaining) + ' request remaining'

print
print 'Finally the end'
